var myGoodsAddCtrl  = new XFApp.client();

XFApp.client.prototype.myGoodsAdd = {
	successCode : 10000,	
	urlItems : {
		submitGoods: basePath+"/userCenter/goods/submitGoods",
		queryGoodsClassURL:basePath+"/userCenter/goods/queryGoodsClass"
	},
	formValidate:function(){
		jQuery.validator.addMethod("isDigit", function(value, element) {
			var length = value.length;
			var isDigit = /^\d+(\.\d{1,5})?$/;
			return this.optional(element) ||  isDigit.test(value);
			}, "必须是数字类型");
		$("#subForm").validate({
			rules: {
				itemno: {
					required: true,
					maxlength: 50
				},
				itemname: {
					required: true,
					maxlength: 200
				},		
				unit:{
					required: true,
				},
				prodweight: {
					required: true,
					isDigit:true
				},
				gmodel: {
					maxlength: 400
				},
				netWeight: {
					required: true,
					isDigit:true
				},
				brand: {
					required: true
				},
				ext4:{
					required: true
				}
			},
			messages: {
				itemno: {
					required: "请输入企业商品货号",
					maxlength: "输入长度超过限制"
				},
				itemname: {
					required: "请输入商品上架品名",
					maxlength: "输入长度超过限制"
				},
				prodweight: {
					required: "请输入产品重量",
					isDigit:"请输入数字类型"
				},
				unit: {
					required: "请输入计量单位",
				},
				gmodel: {
					maxlength: "输入长度超过限制"
				},
				netWeight: {
					required: "请输入产品净重",
					isDigit:"请输入数字类型"
				},
				brand:{
					required: "请输入品牌"
				},
				ext4:{
					required: "请选择商品品类"
				}
			}
		});

	},
	
	event:function(){
		var _this = this;
		_this.formValidate();
		$("#submit_btn").on("click",function(){
			if($("#subForm").validate().form()){
				_this.goodsSubmit();
			}
			_this.vaildForm();
		});
		$("#ext4").on("change",function(){
			if(!!$(this).val()){
				_this.queryGoodsClass($(this).val());
			}else{
				$("#ext6").html('<option value="">请选择</option>');
			}
		});
		
		$("#price").on("keyup",function(){
			if(!!$("#price").val()){
				$("#error_show").html("");
			}
		});
		$("#currency").on("change",function(){
			if(!!$("#currency").val()){
				$("#error_show").html("");
			}
		});
	},
	
	queryGoodsClass:function(parentId){
		var params = {
				parentId:parentId
		}
		var _this = this;
		$.ajax({
			url : _this.urlItems.queryGoodsClassURL,
			dataType : 'json',
			data  : params,
			type : 'post',
			async:false,
			success : function(data) {
				if(data.code==_this.successCode){
					var goodsClassList = data.result;
					if(!parentId){
						$("#ext4").html('<option value="">请选择</option>');
						for(var index = 0;index<goodsClassList.length;index++){
							var goodsClass = goodsClassList[index];
							var dataValue = $("#ext4").attr("data-value");
							if(!! dataValue && dataValue==goodsClass.id){
								$("#ext4").append('<option selected="selected" value="'+goodsClass.id+'">'+goodsClass.name+'</option>');
								_this.queryGoodsClass(goodsClass.id);
							}else{
								$("#ext4").append('<option value="'+goodsClass.id+'">'+goodsClass.name+'</option>');
							}
						}
					}else{
						$("#ext6").html('<option value="">请选择</option>');
						for(var index = 0;index<goodsClassList.length;index++){
							var goodsClass = goodsClassList[index];
							var dataValue = $("#ext6").attr("data-value");
							if(!! dataValue && dataValue==goodsClass.id){
								$("#ext6").append('<option selected="selected" value="'+goodsClass.id+'">'+goodsClass.name+'</option>');
							}else{
								$("#ext6").append('<option value="'+goodsClass.id+'">'+goodsClass.name+'</option>');
							}
						}
					}
				}else{
					webDialogUtil.simpleAlert(data.msg, "温馨提示");
				}
			},
			error:function(){
				webDialogUtil.simpleAlert("加载商品分类信息失败", "温馨提示");
			}
		});
	},
	vaildForm :function(){
		var price = $("#price").val();
		if(!price){
			$("#error_show").html('<label class="error" >请输入价格</label>');
			return false;
		}
		var isDigit = /^\d+(\.\d{1,5})?$/;
		if(!isDigit.test(price)){
			$("#error_show").html('<label class="error">请输入数字类型</label>');
			return false;
		}
		var currency = $("#currency").val();
		if(!currency){
			$("#error_show").html('<label class="error">请输入币制</label>');
			return false;
		}
		return true;
	},
	goodsSubmit:function(){
		var _this = this;
		if(!_this.vaildForm()){
			return;
		}
		webDialogUtil.simpleConfrimAlert("是否确认保存?", function(){
			var params = $("#subForm").serialize().replace(/\+/g, "%20");
			$.ajax({
				url : _this.urlItems.submitGoods,
				dataType : 'json',
				data  : params,
				type : 'post',
				success : function(data) {
					if(data.code==_this.successCode){
						if(!!$("#id").val()){
							window.location.href=basePath+"/userCenter/goods";
						}else{
							webAlert({
					            title: "温馨提示",
					            content: "添加成功",
					            opacity: .5,
					            bg:  "gray",
					            width: 300,
					            height: 10,
					            button:[
					                {
					                    name:"继续添加",
					                    callback:function(){
					                        this.close();
					                    }
					                },{
					                    name:"返回列表",
					                    callback:function(){
					                        this.close();
					                        window.location.href=basePath+"/userCenter/goods";
					                    }
					                }
					            ]
					        });
						}
					}else{
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}
				},
				error:function(){
					webDialogUtil.simpleAlert("保存数据出现异常", "温馨提示");
				}
			});
		}, null, "温馨提示");
	},
	initSelect:function(){
		var _this = this;
		var currencys = dictCtrl.dictionary.translate("currency");//币制
		for(var index = 0;index<currencys.length;index++){
			var currency = currencys[index]
			var dataValue = $("#currency").attr("data-value");
			if(_this.currencyFilter(currency.dictCode)){
				if(!!dataValue && currency.dictCode == dataValue){
					$("#currency").append('<option value="'+currency.dictCode+'" selected="selected">'+currency.dictName+'</option>');
				}else{
					$("#currency").append('<option value="'+currency.dictCode+'">'+currency.dictName+'</option>');
				}
			}
		}
		var units = dictCtrl.dictionary.translate("measureUnit");//单位
		for(var index = 0;index<units.length;index++){
			var unit = units[index]
			var dataValue = $("#unit").attr("data-value");
			if(_this.unitFilter(unit.dictName)){
				if(!!dataValue && unit.dictCode == dataValue){
					$("#unit").append('<option value="'+unit.dictCode+'" selected="selected">'+unit.dictName+'</option>');
				}else{
					$("#unit").append('<option value="'+unit.dictCode+'">'+unit.dictName+'</option>');
				}
			}
		}
		$("#docketstatus").text(goodsConstants.statusConts[$("#docketstatus").attr("data-value")]);
		
		_this.queryGoodsClass();
	},
	currencyFilter:function(dictCode){
		var result = false;
		dictCode = dictCode.trim();
		if(dictCode == '502' || dictCode == '300' || dictCode == '142' || dictCode == '332' || dictCode=='110'){
			result = true;
		}
		return result;
	},
	unitFilter:function(dictName){
		var result = false;
		dictName = dictName.trim();
		var arr = ['个','台','只','件','套','条','卷','码','箱','包','片','双','对','千克','吨','磅','副','幅','组','份'];
		if(arr.indexOf(dictName)!=-1){
			result = true;
		}
		return result;
	}
}
$(function(){
	myGoodsAddCtrl.myGoodsAdd.initSelect();
	myGoodsAddCtrl.myGoodsAdd.event();
});